package dao.impl;

import core.BaseDao;
import dao.LogDao;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.Log;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class LogDaoImpl extends BaseDao implements LogDao {

    /**
     * 查询日志信息
     * @return
     */
    @Override
    public List<Log> findLog() {
        try {
            sql = "SELECT * FROM `log` ORDER BY log_id DESC LIMIT 0,100";
            return queryRunner.query(sql, new BeanListHandler<>(Log.class, rowProcessor));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 添加自定义log日志
     * @param content
     * @return
     */
    @Override
    public Integer addLog(String content, Integer code) {
        try {
            sql = "INSERT INTO log(log_content, log_time, log_is_success) VALUES (?,?,?)";
            return queryRunner.update(sql, content, new Timestamp(System.currentTimeMillis()), code);
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加日志信息(一定成功)
     * @param log
     * @return
     */
    @Override
    public Integer addLog(Log log) {
        try {
            sql = "INSERT INTO log(log_content, log_time, log_worker_name) VALUES (?,?,?)";
            return queryRunner.update(sql, log.getLogContent(), new Timestamp(System.currentTimeMillis()), log.getLogWorkerName());
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 添加日志信息(根据参数)
     * @param log
     * @param code
     * @return
     */
    public Integer addLog(Log log, Integer code) {
        try {
            sql = "INSERT INTO log(log_content, log_time, log_worker_name, log_is_success) VALUES (?,?,?)";
            return queryRunner.update(sql, log.getLogContent(), new Timestamp(System.currentTimeMillis()), log.getLogWorkerName(), code);
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
